package controllers;

import models.AppUser;
import models.UserRole;

public class Authentication extends Secure.Security {

	private static AppUser _CONNECTED_USER;
	private static final String _CHECK_USER_AUTH_QUERY = "login = ? AND pass = ?";

	public static boolean authenticate(String username, String password) {
		_CONNECTED_USER = AppUser.find(_CHECK_USER_AUTH_QUERY, username,
				password).first();
		return _CONNECTED_USER != null;
	}

	public static boolean check(String profile) {
		if (_CONNECTED_USER == null) {
			return false;
		}
		if (UserRole.COLLABORATOR.name().equals(profile)) {
			return _CONNECTED_USER.getRole() == UserRole.COLLABORATOR;
		} else if (UserRole.RTU.name().equals(profile)) {
			return _CONNECTED_USER.getRole() == UserRole.RTU;
		} else if (UserRole.COMMERCIAL.name().equals(profile)) {
			return _CONNECTED_USER.getRole() == UserRole.COMMERCIAL;
		} else if (UserRole.RECRUITMENT_MANAGER.name().equals(profile)) {
			return _CONNECTED_USER.getRole() == UserRole.RECRUITMENT_MANAGER;
		}
		return false;
	}
}
